package main

import (
	"encoding/json"
	"fmt"
	"github.com/techoner/gophp/serialize"
	"log"
	"net/url"

	"github.com/haoleiqin/phpserialize"
)

type serObj struct{}

type serializeStruct struct {
	Id    int64
	Price int64
	Name  string
}

func (serObj *serObj) Phpserialize2Json(serialize string) (jsonres string) {
	var serializeMap []interface{}
	err := phpserialize.Unmarshal([]byte(serialize), &serializeMap)
	if err != nil {
		fmt.Println(err)
	}
	var convertData serializeStruct
	var jsonMap []serializeStruct
	var jsonRes string
	listKey := 0
	for _, list := range serializeMap {
		listKey += 1
		keyIndexNumCount := 0
		for vkey, value := range list.(map[interface{}]interface{}) {
			keyIndexNumCount += 1
			switch vkey {
			case "id":
				convertData.Id = value.(int64)
			case "price":
				convertData.Price = value.(int64)
			case "name":
				convertData.Name = value.(string)
				paramNums := 3
				if keyIndexNumCount == paramNums {
					jsonMap = append(jsonMap, convertData)
					if listKey >= len(serializeMap) {
						data, errsjon := json.Marshal(jsonMap)
						if errsjon != nil {
							fmt.Println(errsjon)
						}
						enEscapeUrl, queryUnescapeErr := url.QueryUnescape(string(data))
						if queryUnescapeErr != nil {
							fmt.Println(queryUnescapeErr)
						}
						jsonRes = enEscapeUrl
					}
				}
			}
		}
	}
	return jsonRes
}

func main() {
	unSerialize()

	str := `a:1:{i:0;a:3:{s:2:"id";i:1;s:5:"price";i:80;s:4:"name";s:12:"一个名字";}}`
	s := &serObj{}
	jsonres := s.Phpserialize2Json(str)
	fmt.Println(jsonres)

}

type JudgeData struct {
	MobileMd5          string `json:"mobileMd5"`
	UtmSource          string `json:"utmSource"`
	App                string `json:"app"`
	InnerApp           string `json:"innerApp"`
	Utm                string `json:"utm"`
	ProducerTime       string `json:"producerTime"`
	RequestFloatNumber string `json:"requestFloatNumber"`
}

func unSerialize() {
	str := `O:29:"console\jobs\RabbitmqJudgeJob":7:{s:9:"mobileMd5";s:32:"a74f1b41172a6efd3faf64c7df89d943";s:9:"utmSource";s:12:"信用飞app";s:3:"app";s:3:"xyf";s:8:"innerApp";s:3:"xyf";s:3:"utm";s:3:"xyf";s:12:"producerTime";s:19:"2022-06-06 16:46:27";s:18:"requestFloatNumber";s:23:"20220606164627993316928";}`
	// str := `a:1:{i:0;a:3:{s:2:"id";i:1;s:5:"price";i:80;s:4:"name";s:12:"一个名字";}}`

	var data []interface{}
	phpserialize.Unmarshal([]byte(str), &data)
	fmt.Println(data)

	i, err := serialize.UnMarshal([]byte(str))
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(i)
}
